package com.bone.payment.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bone.common.BaseIbatisDAO;
import com.bone.constant.PayPaymentConstant;
import com.bone.payment.po.PayPaymentRefundment;

/**
 * 退款处理DAO.
 * @author liwenzhan
 *
 */
public class PayPaymentRefundmentDAO extends BaseIbatisDAO {

	/**
	 * 保存退款记录 .
	 * @param payment
	 * @return
	 */
	public Long savePayPaymentRefundment(PayPaymentRefundment payRefundment){
		return (Long)getSqlMapClientTemplate().insert("PAY_PAYMENT_REFUNDMENT.insert", payRefundment);
	}
	
	/**
	 * 更新退款记录 .
	 * @param payment
	 * @return
	 */
	public void updatePayPaymentRefundment(PayPaymentRefundment payRefundment){
		getSqlMapClientTemplate().update("PAY_PAYMENT_REFUNDMENT.update", payRefundment);
	}

	
	/**
	 * 根据订单ID查询退款的记录数.
	 * @param objectId
	 * @return
	 */
	public Long selectPaymentRefundmentCount(final Long objectId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("objectId", objectId);
		return (Long) super.getSqlMapClientTemplate().queryForObject(
				"PAY_PAYMENT_REFUNDMENT.selectPaymentRefundmentCount", map);
	}
	/**
	 * 根据支付的PaymentId查询出来 需要退款的PayPaymentRefundment
	 * @return
	 */
	public List<PayPaymentRefundment> selectPayPaymentRefundmentListByPaymentId(Long paymentId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paymentId", paymentId);
		List<PayPaymentRefundment> paymentRefundmentList = (List) super.getSqlMapClientTemplate().queryForObject(
				"PAY_PAYMENT_REFUNDMENT.selectByParams", map);
		return paymentRefundmentList;
	}
	
	/**
	 * 根据订单ID和退款状态查询退款明细记录.
	 * @return
	 */
	public List<PayPaymentRefundment> selectPayRefundmentListByObjectIdAndStatus(Long objectId,String status){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("objectId", objectId);
		map.put("status", status);
		List<PayPaymentRefundment> paymentRefundmentList = (List) super.getSqlMapClientTemplate().queryForObject(
				"PAY_PAYMENT_REFUNDMENT.selectByParams", map);
		return paymentRefundmentList;
	}
	
	/**
	 *  根据订单ID查询处有效和需要自动退款的退款明细记录.
	 * @param objectId
	 * @return
	 */
	public List<PayPaymentRefundment> selectValidPayRefundmentListByObjectId(Long objectId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("objectId", objectId);
		map.put("status", PayPaymentConstant.PAY_REFUNMENT_SERIAL_STATUS.CREATE.name());
		map.put("isAuto", "true");
		List<PayPaymentRefundment> paymentRefundmentList = (List) super.getSqlMapClientTemplate().queryForObject(
				"PAY_PAYMENT_REFUNDMENT.selectByParams", map);
		return paymentRefundmentList;
	}
	
	/**
	 *  根据订单ID查询处有效和需要自动退款的退款明细记录.
	 * @return
	 */
	public List<PayPaymentRefundment> selectValidPayRefundmentListBy(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", PayPaymentConstant.PAY_REFUNMENT_SERIAL_STATUS.CREATE.name());
		map.put("isAuto", "true");
		List<PayPaymentRefundment> paymentRefundmentList = (List) super.getSqlMapClientTemplate().queryForObject(
				"PAY_PAYMENT_REFUNDMENT.selectByParams", map);
		return paymentRefundmentList;
	}
	
	/**
	 * 根据退款的流水查询出来 需要退款的PayPaymentRefundment
	 * @return
	 */
	public PayPaymentRefundment selectPayPaymentRefundmentListBy(String serial){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("serial", serial);
		map.put("status", PayPaymentConstant.PAY_REFUNMENT_SERIAL_STATUS.CREATE.name());
		map.put("isAuto", "true");
		List<PayPaymentRefundment> paymentRefundmentList = (List) super.getSqlMapClientTemplate().queryForObject(
				"PAY_PAYMENT_REFUNDMENT.selectByParams", map);
		PayPaymentRefundment payPaymentRefundment = new PayPaymentRefundment();
	    if(paymentRefundmentList!=null&&paymentRefundmentList.size()>0){
	    	payPaymentRefundment=paymentRefundmentList.get(0);
	    }
		return payPaymentRefundment;
		
	}
	
}
